package jaux.tank.generator.component;

import java.util.Locale;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.UIManager;

public class TankGeneratorMenuBar extends JMenuBar{

	private static final long serialVersionUID = 1L;
	
	private TankGeneratorFrame tankFrame;
	
	public TankGeneratorMenuBar( TankGeneratorFrame tankFrame) {
		super();
		this.tankFrame = tankFrame;
		initComponents();
	}

	private void initComponents() {
			// 文件菜单
			JMenu fileMenu = new JMenu(tankFrame.getString("menu.file"));
			this.add(fileMenu);

			// 添加文件菜单项
			JMenuItem openItem = new JMenuItem(tankFrame.getString("menu.file.open"));
			openItem.addActionListener(e->tankFrame.openTank());
			fileMenu.add(openItem);

			JMenuItem saveItem = new JMenuItem(tankFrame.getString("menu.file.save"));
			saveItem.addActionListener(e->tankFrame.saveTank());
			fileMenu.add(saveItem);
			
			JMenuItem saveAsItem = new JMenuItem(tankFrame.getString("menu.file.saveAs"));
			saveAsItem.addActionListener(e->tankFrame.saveAsTank());
			fileMenu.add(saveAsItem);

			JMenuItem closeItem = new JMenuItem(tankFrame.getString("menu.file.close"));
			closeItem.addActionListener(e->tankFrame.closeTank());
			fileMenu.add(closeItem);

			// add splitter
			fileMenu.addSeparator();
			
			JMenuItem exitItem = new JMenuItem(tankFrame.getString("menu.file.exit"));
			exitItem.addActionListener(e->tankFrame.exit());
			fileMenu.add(exitItem);

			// 选项菜单
			JMenu optionMenu = new JMenu(tankFrame.getString("menu.options"));
			this.add(optionMenu);

			// 添加选项菜单项
			// 样式菜单
			JMenu styleMenu = new JMenu(tankFrame.getString("menu.options.style"));
			optionMenu.add(styleMenu);
			
			JMenuItem systemStyleItem = new JMenuItem("System");
			systemStyleItem.addActionListener(e->tankFrame.changeStyle(UIManager.getSystemLookAndFeelClassName()));
			styleMenu.add(systemStyleItem);
			
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
				JMenuItem styleItem = new JMenuItem(info.getName());
				styleItem.addActionListener(e->tankFrame.changeStyle(info.getClassName()));
				styleMenu.add(styleItem);
				
			}

			// 语言菜单
			JMenu languageMenu = new JMenu(tankFrame.getString("menu.options.language"));
			optionMenu.add(languageMenu);

			JMenuItem englishItem = new JMenuItem("English");
			englishItem.addActionListener(e->tankFrame.changeLanguage(Locale.ENGLISH));
			languageMenu.add(englishItem);

			JMenuItem chineseItem = new JMenuItem("中文");
			chineseItem.addActionListener(e->tankFrame.changeLanguage(Locale.CHINESE));
			languageMenu.add(chineseItem);

			// 帮助菜单
			JMenu helpMenu = new JMenu(tankFrame.getString("menu.help"));
			this.add(helpMenu);
			
			JMenuItem aboutItem = new JMenuItem(tankFrame.getString("menu.help.about"));
			AboutDialog aboutDialog = new AboutDialog(tankFrame);
			aboutItem.addActionListener(e->aboutDialog.setVisible(true));
			helpMenu.add(aboutItem);
		
	}
	
}
